﻿using Crm.Service.Instance;
using CRM.Infrastructure.Attr;
using CRM.Infrastructure.Dto.Request;
using CRM.Infrastructure.Dto.Response;
using CRM.Infrastructure.Dto.Response.product;
using CRM.Infrastructure.Dto.Response.Room;
using CRM.Infrastructure.EFCore.Mysql;
using CRM.Infrastructure.IOC;
using CRM.Infrastructure.Tools;
using CRM.Service.Interface;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRM.Service.Instance
{
    [Inject_]
    public class Room_Service:Base_Service,I_Room_Service
    {
        private readonly Order_IOC _order_IOC;
        private readonly Products_IOC _products_IOC;
        private readonly Reservation_IOC _reservation_IOC;
        public Room_Service(Reservation_IOC reservation_IOC, Order_IOC order_IOC, Products_IOC products_IOC)
        {
            _order_IOC = order_IOC;
            _products_IOC = products_IOC;
            _reservation_IOC = reservation_IOC;
        }

        public async Task<Api_Response_Dto> Get_Project(int page = 1, int limit = 4)
        {
            var iq = _products_IOC._setmenu_EFCore.QueryAll(out int total, page, limit,
                false, d => d.SCreateTime);
            var data = await iq.Select(d => new Setmenu_Response_Dto
            {
                id = d.SId,
                name = d.SName,
                createtime = d.SCreateTime,
                list = d.SList,
                money = d.SMoney,
            }).ToListAsync();

            var iq2 = _products_IOC._products_EFCore.QueryAll(out int total2, page, limit,
                false, d => d.ProductCreateTime);
            var data2 = await iq2.Select(d => new Product_Response_Dto
            {
                id = d.ProductId,
                name = d.ProductName,
                datetime = d.ProductCreateTime!.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                price = d.Price,
                img = d.ProductImg,
                expalain = d.ProductExplain,
                list = d.ProductList,
                enddatetime2 = d.EndDatetime.Value.ToString("yyyy-MM-dd"),
                startdatetime2 = d.StartDatetime.Value.ToString("yyyy-MM-dd"),
                enddatetime = d.EndDatetime,
                startdatetime = d.StartDatetime,
                discountprice = d.DiscountPrice,
                typeid = d.TypeId,
                salesvolume = d.ProductSalesVolume
            }).ToListAsync();
            return Result(1, "ok", new
            {
                total,
                total2,
                data,
                data2
            });
        }

        public async Task<Api_Response_Dto> Open_Room(OpenRoomRequest request)
        {
            Order order = new Order()
            {
                OId = RandomNumber.GeneraOrderNumber(),
                ODateTime = DateTime.Now,
                OPayment=0,
                OPeople = request.People,
                ORoom= request.Id,
                OIsFinish=0,
                OMoney=0,
            };
            
            foreach (var i in request.Projects)
            {
                var sp = _products_IOC._products_EFCore.QueryAll(d => d.ProductId == i.id);
                var sp2= _products_IOC._setmenu_product_EFCore.QueryAll(d => d.SpSid == i.id);
                foreach (var j in sp)
                {
                    OrderProduct op = new OrderProduct()
                    {
                        OpId = Config.GUID_2(),
                        PId = j.ProductId,
                        OId = order.OId,
                        ODateTime = DateTime.Now,
                        OpMoney = i.price,
                        OpCount = i.quantity,
                        OpPhoto = j.ProductImg,
                        OpName = j.ProductName,
                    };
                    _order_IOC._order_Product_EFCore.Add(op);
                    
                    order.OMoney=order.OMoney + op.OpMoney * op.OpCount;
                    order.OCount = request.Projects.Count;
                }
                foreach (var j in sp2)
                {
                        OrderProduct op = new OrderProduct()
                        {
                            OpId = Config.GUID_2(),
                            PId = j.SpPid,
                            OId = order.OId,
                            ODateTime = DateTime.Now,
                            OpMoney = j.SpMoney,
                            OpCount = j.SpSum,
                            OpPhoto = j.SpImg,
                            OpName = j.SpPid,
                        };
                        _order_IOC._order_Product_EFCore.Add(op);
                        order.OMoney=order.OMoney + op.OpMoney * op.OpCount;
                        order.OCount = request.Projects.Count;
                    
                                
                }

            }
            var room=_order_IOC._room_EFCode.QueryAll(d => d.RId == request.Id).Single();
            room.RState=2;
            _order_IOC._room_EFCode.Update(room);
            _order_IOC._room_EFCode.SaveChanges();
            _order_IOC._order_Product_EFCore.SaveChanges();
            _order_IOC._order_EFCore.Add(order);

            var a=_reservation_IOC._reservation_EFCore.QueryAll(d=>d.RRid== request.Id).Single();
            if (a!= null)
            {
                a.RStatus = 2;
            }
             _reservation_IOC._reservation_EFCore.Update(a);
            _reservation_IOC._reservation_EFCore.SaveChanges();
            var result = await _order_IOC._order_EFCore.SaveChangesAsync();
            return Result(result);
        }

        public Task<Api_Response_Dto> Settlement_Room(string id)
        {
            throw new NotImplementedException();
        }
    }
}
